package leetcode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 30
 * 给定一个字符串 s 和一些长度相同的单词 words。找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置。
 * <p>
 * 注意子串要与 words 中的单词完全匹配，中间不能有其他字符，但不需要考虑 words 中单词串联的顺序。
 * <p>
 *  
 * <p>
 * 示例 1：
 * <p>
 * 输入：
 * s = "barfoothefoobarman",
 * words = ["foo","bar"]
 * 输出：[0,9]
 * 解释：
 * 从索引 0 和 9 开始的子串分别是 "barfoo" 和 "foobar" 。
 * 输出的顺序不重要, [9,0] 也是有效答案。
 * 示例 2：
 * <p>
 * 输入：
 * s = "wordgoodgoodgoodbestword",
 * words = ["word","good","best","word"]
 * 输出：[]
 *
 * @author: cuihao
 * @create: 2020-07-17 00:49
 **/
public class FindSubString {

    static List<Integer> ans = new ArrayList<>();

    public static void main(String[] args) {
        String s = "wordgoodgoodgoodbestword";
        String[] bb = {"word", "good", "best", "word" };
        System.out.println(new FindSubString().findSubstring1(s, bb));
    }

    /**
     * 滑动窗口 + Map
     *
     * @param s
     * @param words
     * @return
     */
    public List<Integer> findSubstring1(String s, String[] words) {
        List<Integer> res = new ArrayList<>();
        if (s == null || s.length() == 0 || words == null || words.length == 0) return res;
        HashMap<String, Integer> map = new HashMap<>();
        int one_word = words[0].length();
        int word_num = words.length;
        int all_len = one_word * word_num;

        for (String word : words) {
            map.put(word, map.getOrDefault(word, 0) + 1);
        }

        for (int i = 0; i < s.length() - all_len + 1; i++) {
            String tmp = s.substring(i, i + all_len);
            HashMap<String, Integer> tmp_map = new HashMap<>();

            for (int j = 0; j < all_len; j += one_word) {
                String w = tmp.substring(j, j + one_word);
                tmp_map.put(w, tmp_map.getOrDefault(w, 0) + 1);
            }
            if (map.equals(tmp_map)) res.add(i);
        }
        return res;
    }


    /**
     * 滑动窗口 + 回溯
     * TODO 代码有问题
     *
     * @param s
     * @param words
     * @return
     */
    public List<Integer> findSubstring(String s, String[] words) {
        if (s == null || s.length() == 0) {
            return ans;
        }
        char[] arr = s.toCharArray();

        int left = 0;
        int len = 0;

        for (String w : words) {
            len += w.length();
        }
        int right = len - 1;

        while (left < arr.length - len) {
            String ss = s.substring(left, right + 1);
            if (match(ss, words)) {
                ans.add(left);
            }
            right++;
        }

        return null;
    }

    /**
     * 回溯
     *
     * @param s
     * @param words
     * @return
     */
    public boolean match(String s, String[] words) {
        return dfs(s, 0, 0, words, new boolean[words.length]);
    }

    public boolean dfs(String s, int index, int count, String[] words, boolean[] used) {
        if (count == words.length && index == s.length()) {
            return true;
        }
        if (index == s.length()) {
            return false;
        }
        for (int i = 0; i < words.length; i++) {
            if (used[i]) {
                continue;
            }
            if (s.substring(index, index + words[i].length()) == words[i]) {
                continue;
            }
            used[i] = true;
            if (dfs(s, index + words[i].length(), count + 1, words, used)) {
                return true;
            }
            used[i] = false;
        }
        return false;
    }
}
